import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'
import { Router } from '@angular/router'
import { CoolLocalStorage } from '../../coolStorage/cool-local-storage'
import { OprbarService } from './services/oprbar.service'
import { CommonUtil } from '../../utils/common.util'
import { Modal } from 'ng-zorro-antd-mobile'

@Component({
  selector: 'cp-oprbar',
  templateUrl: './oprbar.component.html',
  styleUrls: ['./oprbar.component.scss'],
  providers: [Modal]
})
export class OprbarComponent implements OnInit {
  // 类型(project楼盘，layout户型，promotion活动)
  @Input() type: String
  // project楼盘，layout户型，promotion活动的id
  @Input() id: String
  @Input() estateStatus: String
  // 手机号
  @Input() phone: String
  // 项目配置
  @Input() projectDispatch
  // 是否关注
  @Input() _favorite: Boolean
  @Input()
  get favorite() {
    return this._favorite
  }
  set favorite(value) {
    this._favorite = value
    this.favoriteChange.emit(value)
  }
  // 关注事件
  @Output() favoriteChange = new EventEmitter()

  _mode = ''
  workers = []
  isBookEnable = false
  popupShow = false

  constructor(
    private router: Router,
    private storage: CoolLocalStorage,
    private service: OprbarService,
    private utils: CommonUtil,
    private modal: Modal
  ) {}

  // 初始化
  ngOnInit() {
    this.getMode()
    this.getWorkers()
    if (this.estateStatus && this.estateStatus !== 'SaleStatus_Finish') {
      this.isBookEnable = true
    }
  }
  // 判断模式
  getMode() {
    let _mode = ''
    let projectDispatch = this.projectDispatch
    let category = projectDispatch && projectDispatch.Category
    let mode = projectDispatch && projectDispatch.Mode
    let workers = projectDispatch && projectDispatch.Workers
    if (mode && mode == 'CooperationMode_Developer') {
      _mode = 'developer'
    } else if (mode && mode == 'CooperationMode_Broker') {
      if (workers && workers.length > 0) {
        _mode = 'broker'
      } else {
        if (category && category == 'ProjectCategory_FLM') {
          _mode = 'flm'
        } else {
          _mode = 'developer'
        }
      }
    } else {
      _mode = 'flm'
    }
    this._mode = _mode
  }
  // 经纪人过滤
  getWorkers() {
    let workers = this.projectDispatch.Workers
    if (!workers || workers.length === 0) {
      return false
    }
    let data = workers.map(item => {
      let Photo = this.utils.getThumbnail(item.Photo)
      let obj = {
        Id: item.Id,
        Photo: Photo,
        Name: item.Name,
        Organization: item.Organization,
        Score: item.Score / 20,
        Service: item.Service,
        Phone: item.Phone
      }
      return obj
    })
    this.workers = data
  }
  // 分享
  handleShare() {
    this.utils.info('敬请期待')
  }
  // 关注/取消关注
  handleFavorite() {
    let favorite = this.favorite
    this.utils.loading('正在处理...')
    let msg = favorite ? '已取消关注' : '已关注'
    this.service.toggleFavorite(
      this.type,
      this.id,
      favorite,
      res => {
        this.utils.hide()
        if (res.Success && res.Result) {
          this.favorite = !favorite
          this.utils.info(msg)
        }
      },
      () => {
        this.utils.hide()
      }
    )
  }
  // 预约看房&&我感兴趣
  handleOrder(item) {
    let userinfo = this.storage.getObject('userinfo')
    if (!userinfo) {
      Modal.alert('提示', '由于您当前未登录，无法进行此操作，是否前往登录？', [
        { text: '暂不' },
        {
          text: '去登录',
          onPress: () => {
            new Promise(resolve => {
              this.router.navigateByUrl('/register')
              resolve()
            })
          }
        }
      ])
    } else {
      if (item) {
        this.storage.setObject('worker', item)
      }
      this.router.navigate(['/workspace/estate/appoint', { mode: this._mode, id: this.id }])
    }
  }
}
